requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {
    var url = {
        a: "manager/listLabel.do",//显示所有标签列表
        b: "manager/addLabel.do",//新增标签
        c: "manager/deleteLabel.do",//删除标签
        d: "manager/editLabel.do"
    }
    $(".public-List").eq(2).addClass("publicListHover")

    function init() {
        // 显示所有标签列表
        site.send({
            url: url.a,
            //async:false
            data: {},
            callback: function (data) {
                var list = JSON.parse(data)
                var source = $("#tag_set_content_tpl").html();
                var template = Handlebars.compile(source);
                var html = template(list.content);
                $('#tag_set_content').html(html);
                onBindFun();
            }
        })
    }

    init();

    function onBindFun() {
        // 新增标签弹窗
        $(".add_tagSet_btn").click(function () {
            editAndDelectWindow("new");
            event.stopPropagation();
        })
// 编辑标签
        $(".edit_tagSet_Btn").click(function () {
            editAndDelectWindow("edit", $(this).data('id'), $(this).data('tagname'));
            event.stopPropagation()
        })
        $(".tr1 span").click(function () {
            $(this).siblings("ul").slideToggle(300, function () {
                if ($(this).is(":hidden")) {
                    $(this).siblings(".show_hide_logo").text("[+]")
                } else {
                    $(this).siblings(".show_hide_logo").text("[-]")
                }
            })

        })
        // 删除标签
        $(".delect_tagSet_Btn").click(function () {
            site.send({
                url: url.c,
                //async:false
                data: {id: $(this).data("id")},
                callback: function (data) {
                    var list = JSON.parse(data)
                    if (list.type == 1) {
                        Constant.fn.Message('T', list.msg);
                        pjaxReload();
                    } else {
                        Constant.fn.Message('T', list.msg);
                    }
                }
            })

            event.stopPropagation();
        })
    }


    function editAndDelectWindow(state, id, name) {
        if (state == 'new') {
            $(".new_tagSet_window_title").text("新增标签")
            $(".new_tagSet_name").val('')
            $(".new_tagSet_window").css("display", "block");
            editAndDelectCommit('new')
            return;
        }
        if (state == 'edit') {
            $(".new_tagSet_window_title").text("编辑标签")
            $(".new_tagSet_name").val(name)
            $(".new_tagSet_window").css("display", "block");
            editAndDelectCommit('edit', id)
            return;
        }
    }

    function editAndDelectCommit(state, id) {
        if (state == 'new') {
            // 新增标签提交
            $(".new_tagSet_commit").unbind().click(function () {
                site.send({
                    url: url.b,
                    //async:false
                    data: {name: $(".new_tagSet_name").val()},
                    callback: function (data) {
                        var list = JSON.parse(data)
                        if (list.type == 1) {
                            Constant.fn.Message('T', list.msg);
                            pjaxReload();
                        } else {
                            Constant.fn.Message('F', list.msg);
                        }
                    }
                })
            })
            return;
        }

        if (state == 'edit') {
            // 编辑标签提交
            $(".new_tagSet_commit").unbind().click(function () {
                site.send({
                    url: url.d,
                    //async:false
                    data: {
                        name: $(".new_tagSet_name").val(),
                        id: id
                    },
                    callback: function (data) {
                        var list = JSON.parse(data)
                        if (list.type == 1) {
                            Constant.fn.Message('T', list.msg);
                            pjaxReload();
                        } else {
                            Constant.fn.Message('F', list.msg);
                        }
                    }
                })
            })

            return;
        }
    }



})

